/*
 * Copyright (c) 2022, yifengling0. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. No personal names or organizations' names associated with the
 *    Atomthreads project may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE ATOMTHREADS PROJECT AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */


#ifndef _RTCH_
#define _RTCH_

#include "iosdrv.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct RtcWakeAlarm RtcWakeAlarm;
  
struct RtcWakeAlarm
{
    int  enable;               /* 0 = alarm disabled, 1 = alarm enabled */
    int32_t tm_sec;               /* alarm at tm_sec */
    int32_t tm_min;               /* alarm at tm_min */
    int32_t tm_hour;              /* alarm at tm_hour */
    void (*callback)(RtcWakeAlarm *alarm);
};


typedef enum
{
  RTCCLK_Div16    = ((uint8_t)0x00), /*!< (RTC clock) div 16*/
  RTCCLK_Div8     = ((uint8_t)0x01), /*!< (RTC clock) div 8*/
  RTCCLK_Div4     = ((uint8_t)0x02), /*!< (RTC clock) div 4*/
  RTCCLK_Div2     = ((uint8_t)0x03), /*!< (RTC clock) div 2*/
}RtcWakeUpClock;

typedef struct RtcWakeUp RtcWakeUp;

struct RtcWakeUp{
    int enable;
    RtcWakeUpClock clock;
    uint16_t counter;
    void (*callback)(RtcWakeUp *alarm);
};

typedef struct _rtcTime
{       /* date and time components */
  int tm_sec;
  int tm_min;
  int tm_hour;
  int tm_mday;
  int tm_mon;
  int tm_year;
  int tm_wday;
  int tm_yday;
  int tm_isdst;
}RtcTime;

#define RTC_CTRL_GET_DATETIME 0x10            /**< get time */
#define RTC_CTRL_SET_DATETIME 0x11            /**< set time */
#define RTC_CTRL_GET_ALARM    0x12            /**< get alarm */
#define RTC_CTRL_SET_ALARM    0x13            /**< set alarm */
#define RTC_CTRL_SET_WAKEUP   0x14            /**< set alarm */

typedef struct Rtc Rtc;
struct Rtc {
    IOSDrv super;
};

void RtcCreate(Rtc *rtc, PFIOCTL ioctl);
int  RtcSetDatetime(int year, int month, int day, int hour, int minute, int second);
int  RtcGetDatetime(int *year, int *month, int *day, int *hour, int *minute, int *second);
int  RtcSetAlarm(RtcWakeAlarm *alarm);
int  RtcGetAlarm(RtcWakeAlarm *alarm);
void RtcSetWakeUp(RtcWakeUp *wakeup);

#ifdef __cplusplus
}
#endif

#endif  /* _RTCH_ */